package org.blogsite.greencode.dao.impl;

import java.util.List;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.blogsite.greencode.bean.InstructorManage_Instructor;
import org.blogsite.greencode.bean.Schooling_Instructor_Course;
import org.blogsite.greencode.bean.TeachingSupport_Course;
import org.blogsite.greencode.dao.SchoolingManageDAO;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @author：wzj E-mail: jsun789@gmail.com
 * @date：2010-7-28 下午05:43:01
 */
public class SchoolingManageDAOImpl extends HibernateDaoSupport implements
		SchoolingManageDAO {
	// 日志对象
	Log log = LogFactory.getLog(SchoolingManageDAOImpl.class);

	// 授课单保存
	public void save(Schooling_Instructor_Course schoolingInstructorCourse) {
		try {
//			System.out.println("xxxxx"
//					+ schoolingInstructorCourse.getIm_ino().getId());
			getHibernateTemplate().save(schoolingInstructorCourse);
		} catch (Exception e) {
			log.error("error:::" + e.getMessage());
		}
	}

	// 获取所有课程名称
	public List getAllCourseName() {
		try {
			// getHibernateTemplate().find("select name from InstructorManage_Instructor");
			return getHibernateTemplate().find("from TeachingSupport_Course");

		} catch (Exception e) {
			log.error("error:::" + e.getMessage());
			e.printStackTrace();
		} finally {
			getHibernateTemplate().flush();

		}
		return null;
	}
	// 获取所有讲师姓名
	public List getAllInstructorName() {
		try {
			return getHibernateTemplate().find("from InstructorManage_Instructor");
			
		} catch (Exception e) {
			log.error("error:::" + e.getMessage());
			e.printStackTrace();
		} finally {
			getHibernateTemplate().flush();
			
		}
		return null;
	}

	// 获取指定课程名的详细信息
	public TeachingSupport_Course getCourseById(int id) {
		try {
//		getHibernateTemplate().find("from TeachingSupport_Course course where cno=?",id).iterator().next();
//		System.out.println(getHibernateTemplate().find("from TeachingSupport_Course course where cno=?",id).get(1));
			return (TeachingSupport_Course)getHibernateTemplate().find("from TeachingSupport_Course course where cno=?",id).iterator().next();
//			TeachingSupport_Course sdaf= new TeachingSupport_Course();
//			List list=	getHibernateTemplate().find("select cno,name,credit,period,type,describe from TeachingSupport_Course course where cno=?",id);
//			System.out.println(list.get(0)+"dd");
//			return (TeachingSupport_Course)getHibernateTemplate().find("select cno,name,credit,period,type,describe from TeachingSupport_Course course where cno=?",id).iterator();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error:::" + e.getMessage());
			return null;
		}
	}
	// 获取指定讲师的详细信息
	public InstructorManage_Instructor getInstructorById(int id) {
		try {
			return (InstructorManage_Instructor)getHibernateTemplate().find("from InstructorManage_Instructor instructor where id=?",id).iterator().next();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error:::" + e.getMessage());
			return null;
		}
	}
	//获取所有授课单
	public List getAllTimeTable(){
		try {
			/*
			 * 本来是想要用授课表与课程表和讲师表,进行多表连接查询
			 * 但多表的话,我没们一个bean 是属性都有的,考虑到建立另外的
			 * 表,有点浪费,就想用map或list但查询已经返回一个list,就是list里
			 * 面还有一个list,是没问题,但迭代就比较麻烦了,想不出一种好
			 * 点的方法,常规那样,因为是嵌套迭代,表格就变形,可能有种好点
			 * 的方法,但还没找到,时间原因,只能先放着,但又要把查询信息
			 * 完整点,所以只能先在授课表加上2个字段了.
			 * 
			 *	  return getHibernateTemplate().find("select new List(sic,tsc,imi) from Schooling_Instructor_Course sic,TeachingSupport_Course tsc,InstructorManage_Instructor imi where sic.ts_cno.cno=tsc.cno and sic.im_ino=imi.id ");
			 */
			
			return getHibernateTemplate().find("from Schooling_Instructor_Course sic");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error:::" + e.getMessage());
			return null;
		}
	}
	
	//查询指定课程号授课单
	public List getTimeTableByCno(int id){
		try {
			/*
			 * 因为授课表中,课程号是课程类型,所以传入课程号也只能传入课程
			 * 对象类型,本想用spring注入,到由于id是变量cno是主键,性能上应该
			 * 差不多.
			 */
			
			return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.ts_cno=?",new TeachingSupport_Course(id));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error:::" + e.getMessage());
			return null;
		}
	}
	//查询指定讲师号授课单
	public List getTimeTableByIno(int id){
		try {
			/*
			 * 同上,,查询指定课程号授课单   所述..
			 * 
			 */
			return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.im_ino=?",new InstructorManage_Instructor(id));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error:::" + e.getMessage());
			return null;
		}
	}
	//模糊查询授课单
	public List getTimeTableByBlear(String text){
		try {
//			return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.place like ? or sic","%"+text+"%");
//			return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.place like ? or sic.ts_cno like ? or sic.im_ino like ?","%"+text+"%",new TeachingSupport_Course(Integer.parseInt(text)),new InstructorManage_Instructor(Integer.parseInt(text)));
			
//			try {
//				Integer.parseInt(text);
//			} catch (Exception e) {
//				// TODO: handle exception
//				System.out.println("sdlkfjdsklfjsdklfjsdklfeeeeeeeeeeeeeeeeeeeeeeeeeee");
//				return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.place like ?","%"+text+"%");
//			}
			/*
			 * 这边有个有意思的地方,当搜索的关键字有包含字符,这下面Integer.parseInt(text) 
			 * 就会抛出异常,我们catch他,直接return没有课程号和讲师号的模糊查询,因为课程号
			 * 和讲师号都是整形,既然有字符就没必要参与了..
			 * 			  
			 */
			return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.place like ? or sic.ts_cno like ? or sic.im_ino like ?",new Object[]{"%"+text+"%",new TeachingSupport_Course(Integer.parseInt(text)),new InstructorManage_Instructor(Integer.parseInt(text))});
//			return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.place like ? or sic.ts_cno like ? or sic.im_ino like ?","%"+text+"%");
		} catch (Exception e) {
			System.out.println("continue");
			log.error("info:::" + e.getMessage());
			return getHibernateTemplate().find("from Schooling_Instructor_Course sic where sic.place like ?","%"+text+"%");
		}
	}
	//修改授课单
	public  void updateTimeTable(Schooling_Instructor_Course schoolingInstructorCourse){
		try {
			getHibernateTemplate().update(schoolingInstructorCourse);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error:::" + e.getMessage());
		}
	}
	//删除授课单
	public void deleteTimeTable(Schooling_Instructor_Course schoolingInstructorCourse){
		try {
			getHibernateTemplate().delete(schoolingInstructorCourse);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error:::" + e.getMessage());
		}
		
	}
	
	
	
}
